﻿using Bricks.Business.Models.Account;
using Bricks.Business.Data.Repositories.Account;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Bricks.Component.Utils;
using Bricks.Site.Models.Purview;

namespace Bricks.Business.Service.Impl
{
    [Export(typeof(IRoleInfoService))]
    public class RoleInfoService : IRoleInfoService
    {
        #region 保护属性
        [Import]
        protected IRoleInfoRepository RoleInfoRepository { get; set; }

        [Import]
        protected IRoleMenuActionRepository RoleMenuActionRepository { get; set; }
        #endregion

        #region 公共属性
        public IQueryable<RoleInfo> Roles
        {
            get
            {
                return RoleInfoRepository.Entities;
            }
        }

        public IQueryable<RoleMenuAction> RoleMenuActions
        {
            get
            {
                return RoleMenuActionRepository.Entities;
            }
        } 
        #endregion

        #region 公共方法
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Add(RoleInfo model)
        {
            PublicHelper.CheckArgument(model, "model");
            RoleInfo record = model.CopyTo<RoleInfo>();
            record.ID = GuidHelper.NewGuid();
            record.OrderNo = RoleInfoRepository.GetNextOrderNo(m => m.OrderNo);  //获取下一个排序号

            RoleInfoRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Modify(RoleInfo model)
        {
            PublicHelper.CheckArgument(model, "model");
            RoleInfo record = Roles.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<RoleInfo>();
            if (RoleInfoRepository.Update(record) >= 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult Delete(Guid id)
        {
            RoleInfo record = Roles.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (RoleInfoRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        /// <summary>
        /// 排序设置(向上向下)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pid"></param>
        /// <param name="upOrDown"></param>
        /// <returns></returns>
        public OperationResult SetOrder(Guid id, Guid? pid, string upOrDown)
        {
            IQueryable<RoleInfo> entities = null;
            if (pid.HasValue)
            {
                entities = RoleInfoRepository.Entities.Where(m => m.PID == pid.Value);  //找出可能涉及排序的实体集合（同一PID下）
            }
            else
            {
                entities = RoleInfoRepository.Entities.Where(m => m.PID == null);  //找出可能涉及排序的实体集合（同一PID下）
            }
            var exeResult = RoleInfoRepository.SetOrder(entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
        #endregion
    }
}
